package com.example.administrator.yuejian_2.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.adapter.TrainListAdapter;
import com.example.administrator.yuejian_2.bean.TrainListBean;
import com.example.administrator.yuejian_2.fragment.HomeFragment;
import com.example.administrator.yuejian_2.fragment.TrainFragment;
import com.example.administrator.yuejian_2.interservice.Const;
import com.example.administrator.yuejian_2.interservice.TrainService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class TrainSearchActivity extends AppCompatActivity {

    private EditText train_name;
    private ImageButton back;
    private RecyclerView recyclerView;
    private TrainListAdapter adapter;
    private Context context;
    private Button search_button;
    //搜索结果与培训列表结果一致，因此可以复用TrainListBean类,TrainListViewHolder类以及TrainListAdapter类；

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_train_search);
        train_name = (EditText) findViewById(R.id.train_name);
        back = (ImageButton) findViewById(R.id.trainfragment_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        recyclerView = (RecyclerView) findViewById(R.id.train_all_recyclerView);
        search_button = (Button) findViewById(R.id.search_button);
        search_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchTrain(train_name.getText().toString());
            }
        });
      //searchTrain(train_name.getText().toString());
    }
    public void searchTrain(String name){
        context = TrainSearchActivity.this;
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        TrainService service = retrofit.create(TrainService.class);
        Call<List<TrainListBean>> call = service.search(name);
        call.enqueue(new Callback<List<TrainListBean>>() {
            @Override
            public void onResponse(Call<List<TrainListBean>> call, Response<List<TrainListBean>> response) {
                if (response.body()!= null){
                    adapter = new TrainListAdapter(context, response.body());
                    LinearLayoutManager layoutManager = new LinearLayoutManager(context);
                    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    recyclerView.setLayoutManager(layoutManager);
                    recyclerView.setAdapter(adapter);
                    recyclerView.setItemAnimator(new DefaultItemAnimator());
                }

            }

            @Override
            public void onFailure(Call<List<TrainListBean>> call, Throwable t) {




            }
        });


    }




















}
